#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <gtk/gtk.h>

void gipv6_start()
{
  system("gipv6 --start");
  system("notify-send GIPV6成功启动 IPV6已准备好，Hosts已切换");
  gtk_main_quit();
  return;
}

void gipv6_stop()
{
  system("gipv6 --stop");
  system("notify-send GIPV6成功结束 IPV6隧道已关闭，Hosts已还原");
  gtk_main_quit();
  return;
}

void gipv6_rexec()
{
  system("gipv6 --restart");
  system("notify-send GIPV6已重启动");
  gtk_main_quit();
  return;
}

void gipv6_kill()
{
  system("gipv6 --kill");
  system("notify-send GIPV6进程已被杀死 IPV6隧道已关闭，但Hosts未还原\n请手动还原");
  gtk_main_quit();
  return;
}

void hosts_prep()
{
  system("gipv6 --prep");
  system("notify-send Hosts已被还原");
  gtk_main_quit();
  return;
}

#define LEN 40

void miredo_()
{
  FILE *pp;
  char len[LEN+1];
  pid_t childpid;

  pp = popen( "gipv6 -c", "r");
  if(pp == (FILE *)NULL) system("notify-send 获取信息失败");

  if( (fgets( len, LEN, pp)) != NULL)
    {
      childpid = vfork();
      if(childpid == 0)
        {
          execlp( "notify-send", "notify-send", "GIPV6", len, NULL);
          return;
        }
      else if(childpid > 0)
        {
          waitpid(childpid, NULL, 0);
        }
      else
        {
          fprintf( stderr, "vfork()失败");
        }
    }

  pclose(pp);
  return;
}

void conf_file()
{
    system("notify-send GIPV6-CONFTOOL 请小心配置");
    system("gipv6 --confeditorgtk");
    return;
}
